package com.h3bpm.kumhosunny.sample.controller;

import OThinker.Common.DateTimeUtil;
import OThinker.Common.Organization.Models.User;
import OThinker.H3.Controller.ControllerBase;
import OThinker.H3.Entity.Notification.Notification;
import com.h3bpm.kumhosunny.common.utils.SqlUtil;
import data.DataTable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author zhibiao
 */
@Controller("/Portal/CustomerBackAction")
public class CustomerBackController extends ControllerBase {

    private Logger logger = LoggerFactory.getLogger(CustomerBackController.class);

    @Override
    public String getFunctionCode() {
        return null;
    }

    /***
     * 每天 8:30 点执行一次
     * 查询样品单-客户反馈表 预计反馈时间 < 今天  提醒申请人
     * @return
     */
    @Scheduled(cron = "0 30 8 * * ?")
    //@Scheduled(cron = "0 */5 * * * ?")
    public void TimingTaskAlert() throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String sqlStr = "SELECT ObjectID,applicant,KsOrderFeedbackTime FROM i_customerfeedbacksheet WHERE ksorderfeedbacktime<now()";
        DataTable dataTable = SqlUtil.doQuery(sqlStr);

        if (null != dataTable && dataTable.getRows().size() > 0) {

            for (int i = 0; i < dataTable.getRows().size(); i++) {

                //当前流程申请人
                String userObjectId = dataTable.getRows().get(i).getString("applicant");
                User user = (User) getEngine().getOrganization().GetUnit(userObjectId);

                //ObjectId
                String objcetId = dataTable.getRows().get(i).getString("objectid");

                //获取当前流程预计反馈时间
                Date date = dataTable.getRows().get(i).getDateTime("ksorderfeedbacktime");

                Date nowDate = new Date();

                if (null != date) {
                    if (null != user) {
                        //获取当前流程instanceID
                        String instanceSqlStr="SELECT ObjectID FROM ot_instancecontext WHERE BizObjectId='" + objcetId + "'";
                        String instanceId = SqlUtil.doQuery(instanceSqlStr).getRows().get(0).getString("objectid");

                        //获取当前流程workitemID
                        DataTable workTable= SqlUtil.doQuery(String.format("select  objectid from OT_workitem where instanceId = '%s' limit 1", instanceId));
                        String workItemId = null;

                        if(workTable.getRows().size()>0){
                            workItemId=workTable.getRows().get(0).getString("objectid");
                        }

                        //发送消息
                        if(null!=workItemId && !"".equals(workItemId)){
                            //条件成立,给用户推送钉钉消息
                            String pc_url = "http://www.kumhosunny.cn:8099/Portal/template/ddhome.html?loginfrom=dingtalk&WorkItemID=" + workItemId + "&time=" + System.currentTimeMillis();
                            String mobile_url = "http://www.kumhosunny.cn:8099/Mobile/#/dingtalkmsg/" + workItemId + "?time=" + System.currentTimeMillis();


                            Notification notification = new Notification(OThinker.H3.Entity.Notification.NotifyType.DingTalk, null, user.getObjectID(),
                                    user.getDingTalkAccount(), null, 0, instanceId,
                                    "样品开发-客户反馈表未完成提醒", user.getName() + ",您有一条流程待处理,点击查看", null, mobile_url, pc_url,null);

                            getEngine().getNotifier().Notify(notification);
                            logger.info("[样品开发-客户反馈表]超时未完成流程,负责人:" + user.getFullName() + ",当前时间:" + DateTimeUtil.getDateToString(new Date()));
                        }

                    }
                }


            }


        }
    }


}
